const {nanoid}=require("nanoid")
const {sqlConnect}=require("../model/index")
let express = require("express")
const { SuccessModel, ErrorModel } = require("../utils/resModule");
clientRouter=express.Router()
clientRouter.put('/admin/client-info/:id',function(req,res){
  console.log(req.params)
  console.log("info PUT请求到了服务器,向数据库更改一条数据")
  const {id}=req.params
  const new_set={id,...req.body}
  console.log(new_set)
  var sql=`update person SET ? where id='${id}'`
  sqlConnect.query(sql,new_set,function(err,result){
      if(err){
          console.log('[UPDATE ERROR] - ',err.message);
          return;
      }
      res.json({
          success:true
      })
  })
})
clientRouter.post('/admin/client-info',function(req,res){
    console.log("client-POST请求到了服务器,向数据库插入一条新数据")
    console.log(req.body)
    var {data}=req.body
    const id=nanoid(10)
    const new_set={id,...data}
    console.log(new_set)
    var sql=`insert into person set ?`
    sqlConnect(sql,new_set,function (err) {
        if(err){
          console.log('[INSERT ERROR] - ',err.message);
          if(err.message==`ER_DUP_ENTRY: Duplicate entry '330922199908310020' for key 'person.idno'`)
          {
            res.json({
                data:"该身份证号已存在,用户注册失败",
                success:false
            })
          }
          return;
        }
       res.json({
        success: true
      })
    }
)
})
clientRouter.get('/admin/client-info',function(req,res){
    var sql=`select * from person`
    sqlConnect.query(sql,function(err,result){
        if(err){
            console.log('[SELECT ERROR] - ',err.message);
            return;
          }
         console.log('--------------------------SELECT----------------------------');
         console.log(result);
         console.log('------------------------------------------------------------\n\n');  
         res.json({
          data:result,
          success: true,
        });
    })
})
clientRouter.get('/admin/client-info/search',async function(req,res){
  console.log('client GET-search请求到了服务器')
  console.log(req.query)
  var sql=`select * from person where 1=1 `
  const {fullname,telephone,idno}=req.query
  sql+=fullname?` and fullname="${fullname}"`:'' +telephone? ` and telephone="${telephone}"`:''+idno?`and idno=${idno}`:''
  console.log(sql)
  await sqlConnect.query(sql,function (err, result) {
      if(err){
        console.log('[SELECT ERROR] - ',err.message);
        return;
      }
     console.log('--------------------------SELECT----------------------------');
     //console.log(result);
     console.log('------------------------------------------------------------\n\n');  
     res.json({
      data:result,
      success: true,
    })})
})
clientRouter.get('/admin/client-info/select-itinerary/',async function(req,res){
  console.log('client GET-search请求到了服务器')
  console.log(req.query)
  let {id}=req.query
  var sql=`select DATE_FORMAT(s_time,'%Y-%m-%d') as s_date,fullname,flight_id,seat_no, a.location_name as departure_name,b.location_name as destination_name
  from location as a,location as b,airline_info,person,seat
  where person.id=seat.p_id and seat.a_id=airline_info.id and a.id=airline_info.s_loc_id and b.id=airline_info.e_loc_id
  and p_id= ?`
  sqlConnect.query(sql,id,function(err,result){
      if(err){
        console.log('[SELECT ERROR] - ',err.message);
        return;
      }
    console.log('--------------------------SELECT----------------------------');
    console.log(result);
    console.log('------------------------------------------------------------\n\n');  
    res.json({
      data:result,
      success: true,
    });
  })
})
module.exports={
    clientRouter
}